import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: "/course",
    component: Layouts,
    redirect: "/course/index",
    meta: {
      title: "课程管理",
      svgIcon: "unocss",
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/course/index.vue"),
        // name: "courseIndex",
        meta: {
          title: "课程列表",
          keepAlive: true
        }
      },
      {
        path: "chapter",
        component: () => import("@/views/course/chapter.vue"),
        name: "courseChapter",
        meta: {
          title: "章节管理",
          hidden: true,
          keepAlive: true,
          activeMenu: "/course/index"
        }
      },
      {
        path: "classify",
        component: () => import("@/views/course/classify.vue"),
        name: "courseClassify",
        meta: {
          title: "课程分类",
          keepAlive: true
        }
      },
      {
        path: "instructor",
        component: () => import("@/views/course/instructor.vue"),
        name: "courseInstructor",
        meta: {
          title: "课程讲师",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/exam",
    component: Layouts,
    redirect: "/exam/index",
    meta: {
      title: "考试管理",
      svgIcon: "unocss",
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/exam/index.vue"),
        name: "examIndex",
        meta: {
          title: "考试列表",
          keepAlive: true
        }
      },
      {
        path: "paper",
        component: () => import("@/views/system/dept.vue"),
        name: "examPaper",
        meta: {
          title: "试卷管理",
          keepAlive: true
        }
      },
      {
        path: "group",
        component: () => import("@/views/exam/group.vue"),
        name: "examGroup",
        meta: {
          title: "题库管理",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/news",
    component: Layouts,
    redirect: "/news/index",
    meta: {
      title: "新闻资讯",
      svgIcon: "unocss",
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/news/index.vue"),
        name: "newsIndex",
        meta: {
          title: "新闻列表",
          keepAlive: true
        }
      },
      {
        path: "category",
        component: () => import("@/views/news/category.vue"),
        name: "newsCategory",
        meta: {
          title: "新闻分类",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/system",
    component: Layouts,
    redirect: "/system/user",
    meta: {
      title: "系统管理",
      svgIcon: "unocss",
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "user",
        component: () => import("@/views/system/user.vue"),
        name: "systemUser",
        meta: {
          title: "用户管理",
          keepAlive: true
        }
      },
      {
        path: "dept",
        component: () => import("@/views/system/dept.vue"),
        name: "systemDept",
        meta: {
          title: "部门管理",
          keepAlive: true
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = []

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
